import { useMenuStore } from '@/stores'
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import routes from './modules'
import NProgress from 'nprogress'

const isProd = import.meta.env.PROD
NProgress.configure({ showSpinner: false })
const router = createRouter({
  history: isProd
    ? createWebHashHistory(import.meta.env.BASE_URL)
    : createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior() {
    return {
      top: 0
    }
  }
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  const menuStore = useMenuStore()
  const { set_current } = menuStore
  NProgress.start()
  if (to.matched.length === 0) next('/404')
  if (to.meta.menukey) {
    set_current(to.meta.menukey as string)
  }
  next()
})
router.afterEach(() => {
  NProgress.done()
})
export default router
